package com.eims.sls_app.ui.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Html;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import bls.juejin.cn.R;

import com.eims.sls_app.view.MyListView2;

public class OrderInfoJcPlayTypeAdapter extends BaseAdapter {
    private Context context;
    private List<String> playtypes;
    private List<String> playResult;
    private List<String> playLet;
    private List<List<Map<String, Object>>> playitems;
    private boolean isMy;
    private String listPeilv;

    public OrderInfoJcPlayTypeAdapter(Context context, List<String> playtypes,
                                      List<String> playResult, List<List<Map<String, Object>>> playitems,
                                      List<String> playLet, boolean isMy, String listPeilv) {
        this.context = context;
        this.playtypes = playtypes;
        this.playResult = playResult;
        this.playitems = playitems;
        this.playLet = playLet;
        this.isMy = isMy;
        this.listPeilv = listPeilv;



        Log.e("-----", "OrderInfoJcPlayTypeAdapter: " + listPeilv);
        Log.e("-----", "OrderInfoJcPlayTypeAdapter: " + playtypes.toString());

    }

    public OrderInfoJcPlayTypeAdapter(Context context, List<String> playtypes,
                                      List<String> playResult, List<List<Map<String, Object>>> playitems,
                                      List<String> playLet, boolean isMy) {
        this.context = context;
        this.playtypes = playtypes;
        this.playResult = playResult;
        this.playitems = playitems;
        this.playLet = playLet;
        this.isMy = isMy;


    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return playtypes.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return playtypes.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.orderinfo_jc_child_item, null);
            holder = new ViewHolder();
            holder.lin_playtpe = (TextView) convertView
                    .findViewById(R.id.lin_playtpe);
            holder.orderinfo_jc_child_listview = (MyListView2) convertView
                    .findViewById(R.id.orderinfo_jc_child_listview);
            holder.child_result = (TextView) convertView
                    .findViewById(R.id.child_result);
            // holder.orderinfo_jc_child_listview_result =
            // (MyListView2)convertView.findViewById(R.id.orderinfo_jc_child_listview_result);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.lin_playtpe.setText(playtypes.get(position));

        Log.e("OrderInfoJcPlayTypeAdapter", "getView: " + playtypes.get(position));
        // holder.lin_playtpe.setText(playtypes.get(position)+"(+"+playLet.get(position)+")");
        if (playtypes.get(position).equals("让球")) {
            System.out.println(playLet.get(position));
            holder.lin_playtpe.setText(playtypes.get(position) + "(" + playLet.get(position) + ")");


        } else if (playtypes.get(position).equals("让分")) {
            holder.lin_playtpe.setText(Html.fromHtml(playtypes.get(position)
                    + "<br>" + playLet.get(position) + ""));
        }

        holder.child_result.setText(playResult.get(position));
        if (playResult.get(position).equals("待开奖")) {


        }else {

            if (listPeilv != null) {
                if(listPeilv.startsWith("45")){

                    String[] infos = listPeilv.split(",");
                    Log.e("OrderInfoJcPlayTypeAdapter", position+"getView: " + listPeilv);
                    if (infos[0].startsWith("45")) {

                        holder.child_result.setText(Html.fromHtml(playResult.get(position) + "<br>"
                                + infos[1]));

                    }

                }


            }

        }




        Log.e("OrderInfoJcPlayTypeAdapter", "getView: " + playResult.get(position));
        if (isMy) {
            if (playResult.get(position).equals("待开奖")) {

                holder.orderinfo_jc_child_listview.setAdapter(new InnerAdapter(playitems.get(position), playResult, false, true, playResult.get(position)));
            } else {
                holder.orderinfo_jc_child_listview.setAdapter(new InnerAdapter(playitems.get(position), playResult, true, true, playResult.get(position)));
            }
        } else {
            if (playResult.get(position).equals("待开奖")) {

                holder.orderinfo_jc_child_listview.setAdapter(new InnerAdapter(playitems.get(position), playResult, false, false, playResult.get(position)));
            } else {
                holder.orderinfo_jc_child_listview.setAdapter(new InnerAdapter(playitems.get(position), playResult, true, false, playResult.get(position)));
            }
        }
        return convertView;
    }

    private class ViewHolder {
        TextView lin_playtpe;
        TextView child_result;
        MyListView2 orderinfo_jc_child_listview;
    }

    private class InnerAdapter extends BaseAdapter {
        List<Map<String, Object>> maps = null;
        List<String> result;

        Map<String, String> changeMap = new HashMap<>();

        boolean isOpen;
        boolean is_my;
        String re;

        public InnerAdapter(List<Map<String, Object>> maps, List<String> result, boolean isOpen, boolean is_my, String re) {
            this.maps = maps;
            this.result = result;
            this.isOpen = isOpen;
            this.is_my = is_my;
            this.re = re;

        }

        @Override
        public int getCount() {
            return maps.size();

        }

        @Override
        public Object getItem(int position) {
            return maps.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {
            InnerViewHolder holder = null;


            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.orderinfo_jc_child2_item, null);
                holder = new InnerViewHolder();
                holder.child_invest = (TextView) convertView
                        .findViewById(R.id.child_invest);
                convertView.setTag(holder);
            } else {
                holder = (InnerViewHolder) convertView.getTag();
            }
			/*holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
					"select")
					+ "<br><font color=\"#008000\">"
					+ maps.get(position).get("odd") + "</font>"));*/


            holder.child_invest.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {

                    final TextView tvIno = (TextView) view;


                    final EditText inputServer = new EditText(context);
                    inputServer.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("请输入出票赔率").setView(inputServer)
                            .setNegativeButton("取消", null);
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {

                            if (inputServer.getText().toString() != null && !inputServer.getText().toString().equals("")) {
                                double changeTv = Double.parseDouble(inputServer.getText().toString());

                                double cz = (Double) maps.get(position).get("odd") - changeTv;
                                //changeMap.put(""+position,maps.get(position).get("odd")+"");

                                if (cz >= (-0.2) && cz <= 0.2) {
                                    tvIno.setText(Html.fromHtml(maps.get(position).get(
                                            "select")
                                            + "<br><font color=\"#616161\">"
                                            + inputServer.getText().toString() + "</font>"));
                                    //tvIno.setText(inputServer.getText().toString());
//								maps.get(position).put("odd",inputServer.getText().toString());
////
////
////								notifyDataSetChanged();


                                    //inputText=inputServer.getText().toString();


//								changeMap.put(""+position,inputServer.getText().toString());
//
//		notifyDataSetChanged();


                                } else {
                                    //Toast.makeText(context,"修改最新赔率请在0.2之间",Toast.LENGTH_SHORT).show();

                                }

                            }


                        }
                    });
                    builder.show();

                    return false;
                }
            });

            holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                    "select")
                    + "<br><font color=\"#616161\">"
                    + maps.get(position).get("odd") + "</font>"));


            if (is_my) {

                if (result == null) {
                    holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                            "select")
                            + "<br><font color=\"#616161\">"
                            + maps.get(position).get("odd") + "</font>"));
                } else if (result.get(0).equals("待开奖")) {
                    holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                            "select")
                            + "<br><font color=\"#616161\">"
                            + maps.get(position).get("odd") + "</font>"));
                } else if (result.size() < maps.size()) {
                    Log.e("time13", result.toString() + "," + maps.get(position).toString());
                    if (result.get(0).equals(maps.get(position).get("select"))) {

                        holder.child_invest.setText(Html
                                .fromHtml("<font color=\"#BE0205\">"
                                        + maps.get(position).get("select")
                                        + "</font><br><font color=\"#BE0205\">"
                                        + maps.get(position).get("odd") + "</font>"));
                    } else {
                        holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                "select")
                                + "<br><font color=\"#616161\">"
                                + maps.get(position).get("odd") + "</font>"));
                    }


                } else if (result.size() > 1) {
//					Log.e("time1", result.toString()+","+maps.get(position).toString());
//					Log.e("time1", position+"");
//					Log.e("time1", re+"");
//					for (int i = 0; i < result.size(); i++) {
                    if (re.equals(maps.get(position).get("select"))) {
//							Log.e("time1", result.get(i));
                        holder.child_invest.setText(Html
                                .fromHtml("<font color=\"#BE0205\">"
                                        + maps.get(position).get("select")
                                        + "</font><br><font color=\"#BE0205\">"
                                        + maps.get(position).get("odd") + "</font>"));
                    } else {

//							Log.e("time12", result.get(i));
//							Log.e("time12", maps.get(position).get("select")+"");


                        if (re.contains("大") || re.contains("小")) {
                            String mapString = (String) maps.get(position).get("select");
                            //String[] aStrings= mapString.split("(");
                            if (re.contains("大")) {
                                if (mapString.contains("大")) {
                                    holder.child_invest.setText(Html
                                            .fromHtml("<font color=\"#BE0205\">"
                                                    + maps.get(position).get("select")
                                                    + "</font><br><font color=\"#BE0205\">"
                                                    + maps.get(position).get("odd") + "</font>"));
                                } else {
                                    holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                            "select")
                                            + "<br><font color=\"#616161\">"
                                            + maps.get(position).get("odd") + "</font>"));
                                }
                            }
                            if (re.contains("小")) {
                                if (mapString.contains("小")) {
                                    holder.child_invest.setText(Html
                                            .fromHtml("<font color=\"#BE0205\">"
                                                    + maps.get(position).get("select")
                                                    + "</font><br><font color=\"#BE0205\">"
                                                    + maps.get(position).get("odd") + "</font>"));
                                } else {
                                    holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                            "select")
                                            + "<br><font color=\"#616161\">"
                                            + maps.get(position).get("odd") + "</font>"));
                                }
                            }
                        } else {
                            holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                    "select")
                                    + "<br><font color=\"#616161\">"
                                    + maps.get(position).get("odd") + "</font>"));
                        }

                    }
//					}
                } else if (result.get(position).contains("大") || result.get(position).contains("小")) {
                    String mapString = (String) maps.get(position).get("select");
                    //String[] aStrings= mapString.split("(");
                    if (result.get(position).contains("大")) {

                        if (mapString.contains("大")) {
                            Log.e("xxxxx", "-------2");
                            holder.child_invest.setText(Html
                                    .fromHtml("<font color=\"#BE0205\">"
                                            + maps.get(position).get("select")
                                            + "</font><br><font color=\"#BE0205\">"
                                            + maps.get(position).get("odd") + "</font>"));
                        } else {
                            holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                    "select")
                                    + "<br><font color=\"#616161\">"
                                    + maps.get(position).get("odd") + "</font>"));
                        }
                    }
                    if (result.get(position).contains("小")) {
                        if (mapString.contains("小")) {
                            holder.child_invest.setText(Html
                                    .fromHtml("<font color=\"#BE0205\">"
                                            + maps.get(position).get("select")
                                            + "</font><br><font color=\"#BE0205\">"
                                            + maps.get(position).get("odd") + "</font>"));
                        } else {
                            holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                    "select")
                                    + "<br><font color=\"#616161\">"
                                    + maps.get(position).get("odd") + "</font>"));
                        }
                    }
                } else if (result.get(position).equals(maps.get(position).get("select"))) {

                    holder.child_invest.setText(Html
                            .fromHtml("<font color=\"#BE0205\">"
                                    + maps.get(position).get("select")
                                    + "</font><br><font color=\"#BE0205\">"
                                    + maps.get(position).get("odd") + "</font>"));
                } else {
                    holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                            "select")
                            + "<br><font color=\"#616161\">"
                            + maps.get(position).get("odd") + "</font>"));
                }


            } else {
                if (!isOpen) {
                    holder.child_invest.setText(Html.fromHtml("保密"));
                } else {


                    if (result == null) {
                        holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                "select")
                                + "<br><font color=\"#616161\">"
                                + maps.get(position).get("odd") + "</font>"));
                    } else if (result.get(0).equals("待开奖")) {
                        holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                "select")
                                + "<br><font color=\"#616161\">"
                                + maps.get(position).get("odd") + "</font>"));
                    } else if (result.size() < maps.size()) {

                        if (result.get(0).equals(maps.get(position).get("select"))) {

                            holder.child_invest.setText(Html
                                    .fromHtml("<font color=\"#BE0205\">"
                                            + maps.get(position).get("select")
                                            + "</font><br><font color=\"#BE0205\">"
                                            + maps.get(position).get("odd") + "</font>"));
                        } else {
                            holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                    "select")
                                    + "<br><font color=\"#616161\">"
                                    + maps.get(position).get("odd") + "</font>"));
                        }


                    } else if (result.size() > 1) {
                        Log.e("time1", result.size() + "");
                        Log.e("time1", result.toString() + "");
                        Log.e("time1", maps.get(position).toString() + "");
//					for (int i = 0; i < result.size(); i++) {
//						if (result.get(i).equals(maps.get(position).get("select"))) {
//							
//							holder.child_invest.setText(Html
//									.fromHtml("<font color=\"#BE0205\">"
//											+ maps.get(position).get("select")
//											+ "</font><br><font color=\"#BE0205\">"
//											+ maps.get(position).get("odd") + "</font>"));
//							break;
//						}else{
//							holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
//									"select")
//									+ "<br><font color=\"#616161\">"
//									+ maps.get(position).get("odd") + "</font>"));
//						}
//					}
                        if (re.equals(maps.get(position).get("select"))) {
//						Log.e("time1", result.get(i));
                            holder.child_invest.setText(Html
                                    .fromHtml("<font color=\"#BE0205\">"
                                            + maps.get(position).get("select")
                                            + "</font><br><font color=\"#BE0205\">"
                                            + maps.get(position).get("odd") + "</font>"));
                        } else {

//						Log.e("time12", result.get(i));
                            Log.e("time12", maps.get(position).get("select") + "");
                            holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                    "select")
                                    + "<br><font color=\"#616161\">"
                                    + maps.get(position).get("odd") + "</font>"));
                        }
                    } else if (result.get(position).contains("大") || result.get(position).contains("小")) {
                        String mapString = (String) maps.get(position).get("select");
                        //String[] aStrings= mapString.split("(");
                        if (result.get(position).contains("大")) {
                            if (mapString.contains("大")) {
                                Log.e("xxxxx", "-------5");
                                holder.child_invest.setText(Html
                                        .fromHtml("<font color=\"#BE0205\">"
                                                + maps.get(position).get("select")
                                                + "</font><br><font color=\"#BE0205\">"
                                                + maps.get(position).get("odd") + "</font>"));
                            } else {
                                Log.e("xxxxx", "-------6");
                                holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                        "select")
                                        + "<br><font color=\"#616161\">"
                                        + maps.get(position).get("odd") + "</font>"));
                            }
                        }
                        if (result.get(position).contains("小")) {
                            if (mapString.contains("小")) {
                                holder.child_invest.setText(Html
                                        .fromHtml("<font color=\"#BE0205\">"
                                                + maps.get(position).get("select")
                                                + "</font><br><font color=\"#BE0205\">"
                                                + maps.get(position).get("odd") + "</font>"));
                            } else {
                                holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                        "select")
                                        + "<br><font color=\"#616161\">"
                                        + maps.get(position).get("odd") + "</font>"));
                            }
                        }
                    } else if (result.get(position).equals(maps.get(position).get("select"))) {

                        holder.child_invest.setText(Html
                                .fromHtml("<font color=\"#BE0205\">"
                                        + maps.get(position).get("select")
                                        + "</font><br><font color=\"#BE0205\">"
                                        + maps.get(position).get("odd") + "</font>"));
                    } else {
                        holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
                                "select")
                                + "<br><font color=\"#616161\">"
                                + maps.get(position).get("odd") + "</font>"));
                    }


                }


            }


//			if ( changeMap.get(""+position)!=null){
//
//				holder.child_invest.setText(Html.fromHtml(maps.get(position).get(
//						"select")
//						+ "<br><font color=\"#616161\">"
//						+ changeMap.get(""+position) + "</font>"));
//
//
//			}


            return convertView;
        }

        private class InnerViewHolder {
            TextView child_invest;
        }
    }

}
